<?php
/**
 * 캐쉬 쓰기 - Normal(File)
 *
 * @package util
 * @subpackage cache
 *
 * @author jylee3@simplexi.com
 * @version 1.0
 */
class utilCacheWriterFile extends utilCacheWriterAbstract implements utilCacheWriterInterface
{
    const CACHE_DIR = '_data';
    const CACHE_EXT = '.dat';

    /**
     * 캐쉬 설정 객체
     * @var utilCacheConfigNormal
     */
    protected $cacheConfig;

    public function __construct(utilCacheConfig $cacheConfig)
    {
        $this->setPath($cacheConfig->sDefaultPath . DS . self::CACHE_DIR)->cacheConfig = $cacheConfig;
    }

    /**
     * 캐쉬 데이터 설정
     *
     * @param    String    $sClassName
     * @param    String    $sKey
     * @param    String    $sData
     *
     * @return    Mixed
     */
    public function setData($sClassName, $sKey, $mData)
    {
        /* var utilCacheWriterParserAbstract */
        $writerParser = $this->getWriterParser($this->cacheConfig->sWriterParser);

        $sEncodedData = $writerParser->encodeData($mData);

        $sCachePath = $this->getCachePath($sClassName, $sKey);

        return $this->setPath($sCachePath)->setCacheData($sCachePath, $sKey, $sEncodedData);
    }

    /**
     * 캐쉬 데이터 설정
     *
     * @param    String    $sClassName
     * @param    String    $sKey
     *
     * @return    Mixed
     */
    public function getData($sClassName, $sKey)
    {
        $sCachePath = $this->getCachePath($sClassName, $sKey);

        $aCacheInfo = $this->getCacheData($sCachePath, $sKey);

        /* var utilCacheWriterParserAbstract */
        $writerParser = $this->getWriterParser($this->cacheConfig->sWriterParser);

        $aCacheInfo['data'] = $writerParser->decodeData($aCacheInfo['data']);

        return $aCacheInfo;
    }

    /**
     * 캐쉬 초기화
     *
     * @param    String $sClassName
     *
     * @return    Boolean
     */
    /*
    public function setClear($sClassName)
    {
        $sCachePath = $this->getCachePath($sClassName);

        return $this->utilFile->rmdir($sCachePath);
    }
    */

    /**
     * 캐쉬 전체 초기화
     *
     * @return Boolean
     */
    /*
    public function setClearAll()
    {
        return $this->utilFile->rmdir($this->cacheConfig->sDefaultPath . DS . self::CACHE_DIR);
    }
    */

    /**
     * 캐쉬 데이터 파일 설정
     *
     * @param    String    $sCachePath
     * @param    String    $sFileName
     * @param    String    $sEncodedData
     *
     * @return    String
     */
    public function setCacheData($sCachePath, $sFileName, $sEncodedData)
    {
        $sCacheDataFile = $this->getCacheDataFile($sCachePath, $sFileName);

        $mResult = file_put_contents($sCacheDataFile, $sEncodedData);

        if ($mResult!==false) {
            return $sCacheDataFile;
        } else {
            return false;
        }
    }

    /**
     * 캐쉬 데이터 파일 반환
     *
     * @param    String    $sCachePath
     * @param    String    $sFileName
     *
     * @return    Mixed
     */
    public function getCacheData($sCachePath, $sFileName)
    {
        $sCacheDataFile = $this->getCacheDataFile($sCachePath, $sFileName);
        $mResult = file_get_contents($sCacheDataFile);

        if ($mResult === false) {
            return false;
        } else {
            return array('path'=>$sCacheDataFile, 'data'=>$mResult);
        }
    }

    /**
     * 캐쉬 디렉토리 반환
     *
     * @param    String    $sClassName
     * @param    String    $sFileName
     *
     * @return    String
     */
    protected function getCachePath($sClassName, $sFileName='')
    {
        $sCacheName = $this->cacheConfig->sCacheName;
        $iDirectoryVariance = $this->cacheConfig->iDirectoryVariance;

        $sCacheManagePath = $this->cacheConfig->sDefaultPath . DS
            . self::CACHE_DIR . DS
            . $sCacheName . DS
            . $sClassName;

        if (!empty($sFileName)) {
            if ($iDirectoryVariance>0) {
                $aDirectoryVariance = array();

                for ($i=0; $i<$iDirectoryVariance; $i++) {
                    $aDirectoryVariance[] = substr($sFileName, $i, 1);
                }

                $sCacheManagePath = $sCacheManagePath. DS . implode(DS, $aDirectoryVariance);
            }
        }

        return $sCacheManagePath;
    }

    /**
     * 캐쉬 파일 경로 반환
     *
     * @param    String    $sCachePath
     * @param    String    $sFileName
     *
     * @return    String
     */
    protected function getCacheDataFile($sCachePath, $sFileName)
    {
        return $sCachePath . DS. $sFileName . utilCacheWriterFile::CACHE_EXT . '.' . strtolower($this->cacheConfig->sWriterParser);
    }

    /**
     * 캐쉬 디렉토리 설정(생성)
     *
     * @param    String    $sPath
     *
     * @return    Mixed
     */
    protected function setPath($sPath)
    {
        if (!is_dir($sPath)) {
            utilFile::getInstance()->mkdir($sPath);
        }

        return $this;
    }
}